/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package	dao;

import Hibernate.HibernateUtil;
import pojo.Comment;
import pojo.Book;
import pojo.Comment;
import pojo.Status;
import pojo.Topic;
import pojo.User;


import java.util.Date;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class CommentDAO extends AbstractDAO {

	@Override
	protected Class getPOJOClass() {
		// TODO Auto-generated method stub
		return Comment.class;
	}
	
    public Comment getCommentByID(int id){
        Comment com = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            com = (Comment)session.get(Comment.class, id);
        }
        catch(HibernateException ex){
            
        }
        finally{
            session.close();
        }
        return com;
    }
    public boolean insert(Book b, User user,String nd){
    	boolean flag= false;
    	List<Comment> l= this.getList();
    	int n;
    	if(l.size()==0) n=1;
    	else
    	{
    		Comment t= l.get(l.size()-1);
    		n= t.getCommentId()+1;
    	}
    	Comment c=new Comment();
    	c.setBook(b);
    	StatusDAO stdao= new StatusDAO();
    	Status st= stdao.getStatusByID(2);
    	c.setStatus(st);
    	c.setUser(user);
    	c.setContent(nd);
    	c.setCommentId(n);
    	Date d= new Date();
    	c.setDate(d);

    	Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.save(c);
			transaction.commit();
			session.flush();
			flag=true;
		} catch (HibernateException ex) {
			// Log the exception
			transaction.rollback();
		}
		finally {
			session.close();
			
		}
		return flag;

    }
    
    public List<Comment> getCommentByBook(Book b)
    {
    	List<Comment> ds=null;
     	Session session = HibernateUtil.getSessionFactory().openSession();
    	String hql = "select s from Comment s where s.book.bookId =:n and s.status.idstatus = 2 ";
    	Query query = session.createQuery(hql).setParameter("n", b.getBookId());
    	ds = (List<Comment>) query.list();
    	return ds;
    }
    public List<Comment> getCommentByBook(Book b,int tt)
    {
    	List<Comment> ds=null;
    	Session session = HibernateUtil.getSessionFactory().openSession();
		String hql = "select s from Comment s where s.book.bookId =:n and s.status =:n1 ";
		Query query = session.createQuery(hql).setParameter("n", b.getBookId())
												.setParameter("n1", tt);
		 ds = (List<Comment>) query.list();
		session.close();
    	
    	return ds;
    }

	public long tinhsotrangChuaDuyet(int kq_soluongcm) {
		Date d= new Date();
		long soTrang = 0;
		long  soLuong = 0;
		Session session = HibernateUtil.getSessionFactory().openSession();
		String hql = String.format("select count(*) from Comment cm where cm.status.idstatus = 1");
		Query query = session.createQuery(hql);
		Object obj = query.uniqueResult();
		soLuong =(Long) obj;
		
		if(kq_soluongcm == -1)
			soTrang = 1;
		else{
			soTrang = soLuong / kq_soluongcm;
			if(soLuong % kq_soluongcm != 0)
				soTrang++;
		}
		session.close();
		
		return soTrang;

	}
	public  List<Comment> layCommentChuaduyet(int trang, int kq_soluongcm)
	{
		List<Comment> ds=null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		Date d= new Date();
		String hql = String.format("select cm from Comment cm where cm.status.idstatus = 1 order by cm.commentId asc");
		Query query = session.createQuery(hql);
		if(kq_soluongcm == -1)
		{
			ds = query.list();
		}
		else{
		query.setFirstResult((trang - 1) * kq_soluongcm);
		query.setMaxResults(kq_soluongcm);
		ds = query.list();
		}
		session.close();
		return ds;		
	}
	
	public  boolean capnhatTrangThaiComment(Comment cm)
	{
		boolean flag= false;	
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();

			session.update(cm);
			transaction.commit();
			flag= true;
		} catch (Exception e) {
			transaction.rollback();
		}
		finally{
		session.close();
		}		
		return flag;
	}

	public boolean XoaComment(Comment cm) {
		// TODO Auto-generated method stub
		boolean flag= false;	
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();

			session.delete(cm);
			transaction.commit();
			flag= true;
		} catch (Exception e) {
			transaction.rollback();
		}
		finally{
		session.close();
		}		
		return flag;
	}
	
	public long tinhsotrangTheoSach(Book b, int kq_soluongcm) {
		Date d= new Date();
		long soTrang = 0;
		long  soLuong = 0;
		Session session = HibernateUtil.getSessionFactory().openSession();
		String hql = String.format("select count(*) from Comment cm where cm.book.bookId =:n");
		Query query = session.createQuery(hql).setParameter("n",b.getBookId());
		Object obj = query.uniqueResult();
		soLuong =(Long) obj;
		
		if(kq_soluongcm == -1)
			soTrang = 1;
		else{
			soTrang = soLuong / kq_soluongcm;
			if(soLuong % kq_soluongcm != 0)
				soTrang++;
		}
		session.close();
		
		return soTrang;

	}
	public  List<Comment> layCommentTheoSach(Book b, int trang, int kq_soluongcm)
	{
		List<Comment> ds=null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		Date d= new Date();
		String hql = String.format("select cm from Comment cm where cm.book.bookId=:n order by cm.commentId asc");
		Query query = session.createQuery(hql).setParameter("n", b.getBookId());
		if(kq_soluongcm == -1)
		{
			ds = query.list();
		}
		else{
		query.setFirstResult((trang - 1) * kq_soluongcm);
		query.setMaxResults(kq_soluongcm);
		ds = query.list();
		}
		session.close();
		return ds;		
	}
	
	public long tinhsotrangTheoUser(User u, int kq_soluongcm) {
		Date d= new Date();
		long soTrang = 0;
		long  soLuong = 0;
		Session session = HibernateUtil.getSessionFactory().openSession();
		String hql = String.format("select count(*) from Comment cm where cm.user.userId=:n");
		Query query = session.createQuery(hql).setParameter("n",u.getUserId());
		Object obj = query.uniqueResult();
		soLuong =(Long) obj;
		
		if(kq_soluongcm == -1)
			soTrang = 1;
		else{
			soTrang = soLuong / kq_soluongcm;
			if(soLuong % kq_soluongcm != 0)
				soTrang++;
		}
		session.close();
		
		return soTrang;

	}
	public  List<Comment> layCommentTheoUser(User u, int trang, int kq_soluongcm)
	{
		List<Comment> ds=null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		Date d= new Date();
		String hql = String.format("select cm from Comment cm where cm.user.userId=:n order by cm.commentId asc");
		Query query = session.createQuery(hql).setParameter("n", u.getUserId());
		if(kq_soluongcm == -1)
		{
			ds = query.list();
		}
		else{
		query.setFirstResult((trang - 1) * kq_soluongcm);
		query.setMaxResults(kq_soluongcm);
		ds = query.list();
		}
		session.close();
		return ds;		
	}

}
